<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\model\WebSite;
use app\common\model\WebSiteCookie;
use think\facade\Env;

class Site extends Base
{
    public function index()
    {
        $where                 = [];
        $this->view->site_list = WebSite::where($where)->paginate(30);
        $this->view->page      = $this->view->site_list->render();
        return $this->fetch();
    }

    public function edit($site_id = 0)
    {
        $website = WebSite::where('site_id', '=', $site_id)->find();
        if (!empty($site_id) && empty($website)) {
            return $this->error('指定数据不存在');
        }
        if ($this->request->isPost()) {
            $result = $this->validate($this->request->post(), [
                'title'       => 'require',
                'url'         => 'require',
                'url_regular' => 'require',
            ], [
                'title'       => '网站名称必填',
                'url'         => '官网地址必填',
                'url_regular' => '网站特征码不能为空',
            ]);
            if ($result !== true) {
                return $this->error($result);
            }
            (new WebSite)
                ->allowField(true)
                ->isUpdate(!empty($website))
                ->save($this->request->post());
            return $this->success('站点数据保存成功！');
        }
        $this->view->site = $website;
        return $this->fetch();
    }

    public function cookie($site_id = 0)
    {
        $website = WebSite::where('site_id', '=', $site_id)->find();
        if (empty($website)) {
            return $this->error('指定数据不存在');
        }
        $this->view->site = $website;
        return $this->fetch();
    }

    public function edit_cookie($site_id = 0, $cookie_id = 0)
    {
        $cookie = WebSiteCookie::where('cookie_id', '=', $cookie_id)->find();
        if (!empty($cookie_id) && empty($cookie)) {
            return $this->error('指定Cookie不存在');
        }
        $website = WebSite::where('site_id', '=', $site_id)->find();
        if (empty($website)) {
            return $this->error('指定网站不存在');
        }
        if ($this->request->isPost()) {
            $post   = $this->request->post();
            $result = $this->validate($post, [
                'content' => 'require',
            ], [
                'title' => 'Cookie内容不能为空',
            ]);
            if ($result !== true) {
                return $this->error($result);
            }
            $content     = [];
            $cookie_data = '# Netscape HTTP Cookie File' . PHP_EOL;
            $cookie_data .= '# https://curl.haxx.se/docs/http-cookies.html' . PHP_EOL;
            $cookie_data .= '# This file was generated by libcurl! Edit at your own risk.' . PHP_EOL . PHP_EOL;
            foreach (explode(';', $post['content']) as $value) {
                $value = explode('=', trim($value));
                if (count($value) != 2 || substr($value[0], 0, 7) == 'Hm_lvt_' || substr($value[0], 0, 8) == 'Hm_lpvt_' || substr($value[0], 0, 5) == '_csrf') {
                    continue;
                }
                $content[] = $value[0] . '=' . $value[1];
                $cookie_data .= '.' . $website['url_regular'] . '	TRUE	/	FALSE	' . (time() + 2592000) . '	' . $value[0] . '	' . $value[1] . PHP_EOL;
            }
            $post['content'] = implode('; ', $content);
            $WebSiteCookie   = new WebSiteCookie;
            $WebSiteCookie
                ->allowField(true)
                ->isUpdate(!empty($cookie))
                ->save($post);
            if (!is_dir(Env::get('runtime_path') . 'site_cookie/')) {
                @mkdir(Env::get('runtime_path') . 'site_cookie', 0777);
            }
            file_put_contents(Env::get('runtime_path') . 'site_cookie/cookie_' . $WebSiteCookie->cookie_id, $cookie_data);
            return $this->success('Cookie保存成功！', url('admin/site/cookie', ['site_id' => $website['site_id']]));
        }
        $this->view->website = $website;
        $this->view->cookie  = $cookie;
        return $this->fetch();
    }

    public function delete($site_id = 0)
    {

        if (!$this->request->isAjax()) {
            return $this->error('请求类型错误');
        }
        $website = WebSite::where('site_id', '=', $site_id)->find();
        if (empty($website)) {
            return $this->error('指定数据不存在');
        }
        $website->delete();
        return $this->success('站点删除成功');
    }

    public function delete_cookie($cookie_id = 0)
    {

        if (!$this->request->isAjax()) {
            return $this->error('请求类型错误');
        }
        $cookie = WebSiteCookie::where('cookie_id', '=', $cookie_id)->find();
        if (empty($cookie)) {
            return $this->error('指定数据不存在');
        }
        $cookie_file = Env::get('runtime_path') . 'site_cookie/cookie_' . $cookie->cookie_id;
        if (is_file($cookie_file)) {
            @unlink($cookie_file);
        }
        $cookie->delete();
        return $this->success('站点删除成功');
    }
}
